﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Grizzy_X
{
    public partial class attri_Table : Form
    {
        private CLayer CurrentQueryLayer;

        //初始化属性表
        public attri_Table()
        {
            InitializeComponent();
            CurrentQueryLayer = Controller.CurrentLayer;
            this.LnamecomboBox.Text = Controller.CurrentLayer.Name;
            for (int i = 0; i < Controller.MyMap.Layer_List.Count; i++)
            {
                LnamecomboBox.Items.Add(Controller.MyMap.Layer_List[i].Name);
            }
            switch (Controller.CurrentLayer.Layer_Type)
            {
                case CLayer.LayerType.CPointLayer:
                    this.LTypeLabel.Text = "点";
                    break;
                case CLayer.LayerType.CLineLayer:
                    this.LTypeLabel.Text = "线";
                    break;
                case CLayer.LayerType.CPolygonLayer:
                    this.LTypeLabel.Text = "多边形";
                    break;
                case CLayer.LayerType.CTextLayer:
                    this.LTypeLabel.Text = "文本";
                    break;
            }
            if (Controller.DBExists == true)
            {
                //显示当前图层的所有对象属性
                Persistance.cmd.CommandText = "SELECT * FROM " + Controller.CurrentLayer.Name;
                Persistance.myDataAdapter.SelectCommand = Persistance.cmd;
                DataSet myDataSet = new DataSet();
                Persistance.myConnection.Open();
                int numberOfRows = Persistance.myDataAdapter.Fill(myDataSet, Controller.CurrentLayer.Name);
                Persistance.myConnection.Close();
                DataTable myDataTable = myDataSet.Tables[Controller.CurrentLayer.Name];
                this.dataGridView1.DataSource = myDataTable;

                //把可查询属性添加进去
                if (Controller.CurrentLayer.AttriFields.Count == 0)
                    attricomboBox.Text = "暂无属性";
                else
                {
                    attricomboBox.Text = Controller.CurrentLayer.AttriFields[0][0];
                    RecomboBox.Text = "=";
                }
                for (int i = 0; i < Controller.CurrentLayer.AttriFields.Count; i++)
                {
                    attricomboBox.Items.Add(Controller.CurrentLayer.AttriFields[i][0]);
                }
            }
            else
            {
                this.dataGridView1.Rows.Add("数据库还未建立。");
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        //改变要查看属性数据的图层
        private void LnamecomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            attricomboBox.Items.Clear();
            //改变可查询的属性
            for (int i = 0; i < Controller.MyMap.Layer_List.Count; i++)
            {
                if (Controller.MyMap.Layer_List[i].Name == LnamecomboBox.Text)
                {//先找到要查询属性的图层
                    CurrentQueryLayer = Controller.MyMap.Layer_List[i];
                    break;
                }
            }
            if (Controller.DBExists == true)
            {
                Persistance.cmd.CommandText = "SELECT 图层类型 FROM 已有图层属性数据 WHERE 图层名称='" + LnamecomboBox.Text+"'";
                Persistance.myConnection.Open();
                string TYPE = Persistance.cmd.ExecuteScalar().ToString();
                switch(TYPE)
                {
                    case "点":
                        this.LTypeLabel.Text = "点";
                        break;
                    case "线":
                        this.LTypeLabel.Text = "线";
                        break;
                    case "多边形":
                        this.LTypeLabel.Text = "多边形";
                        break;
                    case "文本":
                        this.LTypeLabel.Text = "文本";
                        break;
                }
                Persistance.myConnection.Close();
                Persistance.cmd.CommandText = null;

                //显示当前图层的所有对象属性
                Persistance.cmd.CommandText = "SELECT * FROM " + LnamecomboBox.Text;
                Persistance.myDataAdapter.SelectCommand = Persistance.cmd;
                DataSet myDataSet = new DataSet();
                Persistance.myConnection.Open();
                int numberOfRows = Persistance.myDataAdapter.Fill(myDataSet, Controller.CurrentLayer.Name);
                Persistance.myConnection.Close();
                DataTable myDataTable = myDataSet.Tables[Controller.CurrentLayer.Name];
                this.dataGridView1.DataSource = myDataTable;

                if (CurrentQueryLayer.AttriFields.Count == 0)
                    attricomboBox.Text = "暂无属性";
                else
                {
                    attricomboBox.Text = CurrentQueryLayer.AttriFields[0][0];
                    RecomboBox.Text = "=";
                }
                for (int i = 0; i < CurrentQueryLayer.AttriFields.Count; i++)
                {
                    attricomboBox.Items.Add(CurrentQueryLayer.AttriFields[i][0]);
                }
            }
            else
            {
                this.dataGridView1.Rows.Add("数据库还未建立。");
            }
        }

        //改变要查询属性
        private void attricomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            RecomboBox.Items.Clear();
            RecomboBox.Text = "=";
            for (int i = 0; i < CurrentQueryLayer.AttriFields.Count; i++)
            {
                if (attricomboBox.Text == CurrentQueryLayer.AttriFields[i][0])
                {
                    if (CurrentQueryLayer.AttriFields[i][1].Contains("Int") || CurrentQueryLayer.AttriFields[i][1].Contains("Float") || CurrentQueryLayer.AttriFields[i][1].Contains("Byte"))//数值类型，可以">"or"<"or"="
                    {
                        RecomboBox.Items.Add("=");
                        RecomboBox.Items.Add(">");
                        RecomboBox.Items.Add(">=");
                        RecomboBox.Items.Add("<");
                        RecomboBox.Items.Add("<=");
                    }
                    else
                    {
                        RecomboBox.Items.Add("=");
                    }
                }
            }
        }

        private void OKbutton_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == false)
            {
                for (int i = 0; i < CurrentQueryLayer.AttriFields.Count; i++)
                {
                    if (attricomboBox.Text == CurrentQueryLayer.AttriFields[i][0])
                    {
                        if (CurrentQueryLayer.AttriFields[i][1].Contains("Int") || CurrentQueryLayer.AttriFields[i][1].Contains("Float") || CurrentQueryLayer.AttriFields[i][1].Contains("Byte"))//数值类型，可以">"or"<"or"="
                        {
                            Persistance.cmd.CommandText = "SELECT * FROM ";
                            Persistance.cmd.CommandText += LnamecomboBox.Text;
                            Persistance.cmd.CommandText += " WHERE ";
                            Persistance.cmd.CommandText += attricomboBox.Text;
                            Persistance.cmd.CommandText += " " + RecomboBox.Text;
                            Persistance.cmd.CommandText += " " + valuetextBox.Text;
                            Persistance.cmd.CommandText += ";";
                        }
                        else
                        {
                            Persistance.cmd.CommandText = "SELECT * FROM ";
                            Persistance.cmd.CommandText += LnamecomboBox.Text;
                            Persistance.cmd.CommandText += " WHERE ";
                            Persistance.cmd.CommandText += attricomboBox.Text;
                            Persistance.cmd.CommandText += " " + RecomboBox.Text;
                            Persistance.cmd.CommandText += " '" + valuetextBox.Text;
                            Persistance.cmd.CommandText += "';";
                        }
                    }
                }
                try
                {
                    Persistance.myConnection.Open();
                    Persistance.myDataAdapter.SelectCommand = Persistance.cmd;
                    DataSet myDataSet = new DataSet();
                    int numberOfRows = Persistance.myDataAdapter.Fill(myDataSet, "查询");
                    Persistance.myConnection.Close();
                    DataTable myDataTable = myDataSet.Tables["查询"];
                    this.dataGridView1.DataSource = myDataTable;
                }
                catch
                {
                    MessageBox.Show("警告：请输入必要的查询字段或检查查询操作是否正确！");
                }
            }
            else//需要画出选择的图形
            {
                for (int i = 0; i < CurrentQueryLayer.AttriFields.Count; i++)
                {
                    if (attricomboBox.Text == CurrentQueryLayer.AttriFields[i][0])
                    {
                        if (CurrentQueryLayer.AttriFields[i][1].Contains("Int") || CurrentQueryLayer.AttriFields[i][1].Contains("Float") || CurrentQueryLayer.AttriFields[i][1].Contains("Byte"))//数值类型，可以">"or"<"or"="
                        {
                            Persistance.cmd.CommandText = "SELECT * FROM ";
                            Persistance.cmd.CommandText += LnamecomboBox.Text;
                            Persistance.cmd.CommandText += " WHERE ";
                            Persistance.cmd.CommandText += attricomboBox.Text;
                            Persistance.cmd.CommandText += " " + RecomboBox.Text;
                            Persistance.cmd.CommandText += " " + valuetextBox.Text;
                            Persistance.cmd.CommandText += ";";
                        }
                        else
                        {
                            Persistance.cmd.CommandText = "SELECT * FROM ";
                            Persistance.cmd.CommandText += LnamecomboBox.Text;
                            Persistance.cmd.CommandText += " WHERE ";
                            Persistance.cmd.CommandText += attricomboBox.Text;
                            Persistance.cmd.CommandText += " " + RecomboBox.Text;
                            Persistance.cmd.CommandText += " '" + valuetextBox.Text;
                            Persistance.cmd.CommandText += "';";
                        }
                    }
                }
                try
                {
                    Persistance.myConnection.Open();
                    Persistance.myDataAdapter.SelectCommand = Persistance.cmd;
                    DataSet myDataSet = new DataSet();
                    int numberOfRows = Persistance.myDataAdapter.Fill(myDataSet, "查询");
                    Persistance.myConnection.Close();
                    DataTable myDataTable = myDataSet.Tables["查询"];
                    this.dataGridView1.DataSource = myDataTable;

                    MessageBox.Show("已选择图形。");

                    Controller.Selected = new List<int>();
                    for (int i = 0; i < myDataTable.Rows.Count; i++)
                    {
                        switch (CurrentQueryLayer.Layer_Type)
                        {
                            case CLayer.LayerType.CPointLayer:
                                {
                                    for (int j = 0; j < ((CPointLayer)CurrentQueryLayer).Point_Set.Count; j++)
                                    {
                                            if (((CPointLayer)CurrentQueryLayer).Point_Set[j].GetID() == Convert.ToInt32(myDataTable.Rows[i][0]))
                                            {
                                                Controller.Selected.Add(j);
                                            }
                                    }
                                    Controller.FindLayer(CurrentQueryLayer.GetID()).Image = ((CPointLayer)CurrentQueryLayer).drawpoint();
                                    break;
                                }
                            case CLayer.LayerType.CLineLayer:
                                {
                                    for (int j = 0; j < ((CLineLayer)CurrentQueryLayer).Line_Set.Count; j++)
                                    {
                                        if (((CLineLayer)CurrentQueryLayer).Line_Set[j].GetID() == Convert.ToInt32(myDataTable.Rows[i][0]))
                                        {
                                            Controller.Selected.Add(j);
                                        }
                                    }
                                    Controller.FindLayer(CurrentQueryLayer.GetID()).Image = ((CLineLayer)CurrentQueryLayer).drawlinelayer();
                                    break;
                                }
                            case CLayer.LayerType.CPolygonLayer:
                                {
                                    for (int j = 0; j < ((CPolygonLayer)CurrentQueryLayer).Polygon_Set.Count; j++)
                                    {
                                        if (((CPolygonLayer)CurrentQueryLayer).Polygon_Set[j].GetID() == Convert.ToInt32(myDataTable.Rows[i][0]))
                                        {
                                            Controller.Selected.Add(j);
                                        }
                                    }
                                    Controller.FindLayer(CurrentQueryLayer.GetID()).Image = ((CPolygonLayer)CurrentQueryLayer).drawpolygon();
                                    break;
                                }
                            case CLayer.LayerType.CTextLayer:
                                {
                                    for (int j = 0; j < ((CTextLayer)CurrentQueryLayer).Text_Set.Count; j++)
                                    {
                                        if (((CTextLayer)CurrentQueryLayer).Text_Set[j].GetID() == Convert.ToInt32(myDataTable.Rows[i][0]))
                                        {
                                            Controller.Selected.Add(j);
                                        }
                                    }
                                    Controller.FindLayer(CurrentQueryLayer.GetID()).Image = ((CTextLayer)CurrentQueryLayer).drawtext();
                                    break;
                                }
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("警告：请输入必要的查询字段或检查查询操作是否正确！");
                }
            }
        }

        private void cancelbutton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
